package com.lcb.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.account.entity.TAccount;
import com.lcb.account.mapper.TAccountMapper;
import com.lcb.account.service.TAccountService;
import com.lcb.common.utils.BaseApiService;
import com.lcb.common.utils.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;

/****
 * @Author:lcb
 * @Description:TAccount业务层接口实现类
 * @Date: 2021-01-28 11:27:07
 *****/

@RestController
//@RequestMapping("/account")
//@CrossOrigin
public class TAccountServiceImpl extends BaseApiService implements TAccountService {

    @Autowired
    private TAccountMapper tAccountMapper;

    @Override
    public BaseResponse decrease(@RequestBody TAccount tAccount) {

        tAccount.setId(1l);
        tAccount.setTotal(50 + "");
        tAccountMapper.updateByPrimaryKeySelective(tAccount);
        return setResultSuccess("扣减成功");
    }

    /**
     * TAccount条件+分页查询
     *
     * @param tAccount 查询条件
     * @param page     页码
     * @param size     页大小
     * @return 分页结果
     */
    @Override
    public BaseResponse findTAccountList(@RequestBody TAccount tAccount, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        //分页
        PageHelper.startPage(page, size);

        PageInfo<TAccount> pagelist = new PageInfo<TAccount>(tAccountMapper.select(tAccount));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public BaseResponse deleteTAccountById(@PathVariable(value = "ids") String ids) {

        String[] split = ids.split(",");
        Example example = new Example(TAccount.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        tAccountMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改TAccount
     *
     * @param tAccount
     */
    @Override
    public BaseResponse updateTAccount(@RequestBody TAccount tAccount) {
        tAccountMapper.updateByPrimaryKeySelective(tAccount);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加TAccount
     *
     * @param tAccount
     */
    @Override
    public BaseResponse addTAccount(@RequestBody TAccount tAccount) {
        tAccountMapper.insertSelective(tAccount);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询TAccount
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse findTAccountById(@PathVariable(value = "id") Long id) {
        return setResultSuccess(tAccountMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询TAccount全部数据
     *
     * @return
     */
    @Override
    public BaseResponse findTAccountAll() {
        return setResultSuccess(tAccountMapper.selectAll());
    }
}
